#!/usr/bin/env bash
# This script is written for Setup Shadowsock server for users for CentOS6 and later.
yum install deltarpm -y
yum install -y redhat-lsb
centosver=$(lsb_release -rs|awk -F'.' '{print $1}')
if [[ ${centosver} -eq 6 ]]; then
    echo "CentOS版本6"
    echo "开始安装Python3"
    yum install -y wget openssl* gcc* sqlite* python-devel zlib libffi-devel
    yum install gcc libffi-devel zlib* openssl-devel libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make -y
# 下载安装包，如果有更新，请去python官网更新下载网址
wget -c https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
tar zxvf Python-3.6.5.tgz
cd Python-3.6.5
# 之所以不开启优化，是怕出问题，如果你不怕，可以自己添加
./configure --prefix=/usr/local/python3 --enable-optimizations
make && make install
rm -rf /usr/bin/python3
rm -rf /usr/bin/pip3
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
pip3 install shadowsocks
ln -s /usr/local/python3/bin/ssserver /usr/bin/ssserver
cd ..
cp ss.sh /etc/init.d/ss
chmod 755 /etc/init.d/ss
chkconfig ss on
service ss start
iptables -A INPUT -p tcp --dport 10040 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 10040 -j ACCEPT
elif [[ ${centosver} -eq 8 ]];then
yum install -y openssl*
pip3 install shadowsocks
ssPort=10048
ssPass=rffanlab
ssMethod=aes-256-ctr

cat >/usr/lib/systemd/system/ss-server.service <<EOF
[Unit]
Description=ShadowSocks Server

[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/usr/local/bin/ssserver -p $ssPort -k $ssPass -m $ssMethod --user nobody -d start
ExecStop=/usr/bin/ssserver -d stop
Restart=on-abort

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable ss-server
systemctl start ss-server
systemctl status ss-server

firewall-cmd --zone=public --permanent --add-port=$ssPort/tcp
firewall-cmd --zone=public --permanent --add-port=$ssPort/udp
firewall-cmd --reload
else
yum install -y git wget python-devel python-setuptools
easy_install pip
pip install shadowsocks
ssPort=10048
ssPass=rffanlab
ssMethod=aes-256-ctr

cat >/usr/lib/systemd/system/ss-server.service <<EOF
[Unit]
Description=ShadowSocks Server

[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/usr/bin/ssserver -p $ssPort -k $ssPass -m $ssMethod --user nobody -d start
ExecStop=/usr/bin/ssserver -d stop
Restart=on-abort

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable ss-server
systemctl start ss-server
systemctl status ss-server

firewall-cmd --zone=public --permanent --add-port=$ssPort/tcp
firewall-cmd --zone=public --permanent --add-port=$ssPort/udp
firewall-cmd --reload
fi


